package com.dk.controller;

import com.alibaba.fastjson.JSON;
import com.dk.pojo.Car;
import com.dk.pojo.CarType;
import com.dk.pojo.vo.CarVO;
import com.dk.service.CarService;
import com.dk.service.CarTpeyService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("car")
public class CarController {
    @Autowired
    private CarService carService;

    @Autowired
    private CarTpeyService carTpeyService;

    private Logger logger = LoggerFactory.getLogger(CarController.class);

    @RequestMapping("getCarAllInfo")
    @ResponseBody
    public Map<String,Object> getCarAllInfo(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){
        try {
            logger.info("进入分页查询汽车数据方法");
            PageInfo<CarVO> page = carService.getCarAllInfo(pageNum,pageSize);
            logger.info("组装数据返回前台");
            Map<String,Object> map = new HashMap<>();
            map.put("total",page.getTotal());
            map.put("rows",page.getList());
            return map;
        }catch (Exception e){
            logger.error("查询全部汽车数据方法异常：",e);
            return null;
        }
    }

    @RequestMapping("toCarlist")
    public String toCarlist(){
        return "/car/list";
    }

    @RequestMapping("toAddPage")
    public String toAddPage(Model model){
        try {
            logger.info("进入跳转增加汽车页面方法，开始进入查询所有汽车类型数据方法");
            List<CarType> carTypes = carTpeyService.getCarTypeInfo();
            model.addAttribute("carTypes",carTypes);
        }catch (Exception e){
            logger.error("查询所有汽车类型数据异常",e);
        }
        return "/car/add";
    }

    @RequestMapping("addCar")
    @ResponseBody
    public void addCar(Car c){
        try {
            logger.info("进入增加汽车数据方法，参数：{}", JSON.toJSONString(c));
            c.setIsLower(0);
            carService.addCar(c);
        }catch (Exception e){
            logger.error("增加汽车数据异常",e);
        }
    }

    @RequestMapping("toUpdatePage")
    public String toUpdatePage(Integer carId,Model model){
        try {
            logger.info("进入跳转修改汽车页面方法，开始进入查询所有汽车类型数据方法");
            List<CarType> carTypes = carTpeyService.getCarTypeInfo();
            model.addAttribute("carTypes",carTypes);
            logger.info("进入根据汽车ID查询汽车数据方法，参数：{}",carId);
            Car c = carService.getCarById(carId);
            model.addAttribute("c",c);

        }catch (Exception e){
            logger.error("跳转修改页面方法，查询数据方法异常",e);
        }
        return "car/update";
    }

    @RequestMapping("updateCar")
    @ResponseBody
    public void updateCar(Car c){
        try {
            logger.info("进入修改汽车数据方法，参数：{}", JSON.toJSONString(c));
            carService.updateCar(c);
        }catch (Exception e){
            logger.error("修改汽车数据异常",e);
        }
    }

    @RequestMapping("deleteCar")
    @ResponseBody
    public void deleteCar(Integer carId){
        try {
            logger.info("进入根据汽车ID删除汽车数据方法，参数：{}",carId);
            carService.deleteCar(carId);
        }catch (Exception e){
            logger.error("删除汽车数据异常",e);
        }
    }

    @RequestMapping("toCarExamineList")
    public String toCarExamineList(){
        return "car/examine";
    }

    @RequestMapping("getNoExamineCarInfo")
    @ResponseBody
    public List<CarVO> getNoExamineCarInfo(){
        try {
            logger.info("进入查询所有未审核汽车数据方法");
            List<CarVO> list = carService.getNoExamineCarInfo();
            return list;
        }catch (Exception e){
            logger.error("查询所有未审核汽车数据异常",e);
            return null;
        }
    }

    @RequestMapping("examineCar")
    @ResponseBody
    public void examineCar(Integer carId,Integer flag){
        try {
            logger.info("进入汽车审核方法，参数：汽车ID{}，标识{}",carId,flag);
            carService.updateCarIsLowerById(carId,flag);
        }catch (Exception e){
            logger.error("审核汽车方法异常",e);
        }

    }


}
